﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Text.RegularExpressions;

namespace template_dll
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        public SqlConnection conn;
        private DataTable dt_supman = new DataTable();
        private DataTable dt_pharmsup = new DataTable();

        private void Form2_Load(object sender, EventArgs e)
        {
            if(conn.State == ConnectionState.Closed) conn.Open();
            SqlCommand comm = conn.CreateCommand();
            comm.CommandText = "pas_SupManagerSel";
            comm.CommandType = CommandType.StoredProcedure;
            dt_supman.Load(comm.ExecuteReader());
            if(dt_supman.Rows.Count > 0)
                foreach (DataRow row in dt_supman.Rows)
                {
                    comboBox1.Items.Add(row[1].ToString());
                }
            PharmSupManagerSel();
            comboBox1.SelectedIndex = 0;
        }

        private void PharmSupManagerSel()
        {
            dataGridView1.DataSource = null;
            dt_pharmsup = null;
            dt_pharmsup = new DataTable();
            SqlCommand comm = conn.CreateCommand();
            comm.CommandType = CommandType.StoredProcedure;
            comm.CommandText = "pas_PharmSupManagerSel";
            dt_pharmsup.Load(comm.ExecuteReader());
            dataGridView1.DataSource = dt_pharmsup.DefaultView;
            dataGridView1.Sort(dataGridView1.Columns[1], ListSortDirection.Ascending);
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            HideShowRows();
        }

        private void textBox1_Enter(object sender, EventArgs e)
        {
            HideShowRows();
        }

        private void HideShowRows()
        {
            CurrencyManager cm = (CurrencyManager)dataGridView1.BindingContext[dataGridView1.DataSource];
            cm.SuspendBinding();
            foreach (DataGridViewRow r in dataGridView1.Rows)
            {
                r.Visible = true;
                if (!Regex.Match(Convert.ToString(dataGridView1[dataGridView1.SortedColumn.Name, r.Index].Value), textBox1.Text, RegexOptions.IgnoreCase).Success)
                    r.Visible = false;
            }
            cm.ResumeBinding();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                int row = dataGridView1.CurrentRow.Index;
                if (MessageBox.Show("Аптека " + dataGridView1[1, row].Value.ToString() + "\nменеджер " + dataGridView1[2, row].Value.ToString() + "\nна " + comboBox1.Text
                    , "Изменить строку?", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    SqlCommand comm = conn.CreateCommand();
                    comm.CommandType = CommandType.StoredProcedure;
                    comm.CommandText = "pas_PharmCodeSupManUpd";
                    comm.Parameters.AddWithValue("@pharmcode", dataGridView1[0, row].Value);
                    foreach (DataRow r in dt_supman.Rows)
                    {
                        if (r[1].ToString() == comboBox1.Text)
                            comm.Parameters.AddWithValue("@supmanid", r[0]);
                    }
                    if (conn.State == ConnectionState.Closed) conn.Open();
                    comm.ExecuteNonQuery();
                    PharmSupManagerSel();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }
    }
}
